﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Google.GData.YouTube;
using Google.GData.Client;
using FlickrNet;
using Resources;
using Google.GData.Photos;

public partial class Controls_WebContent : System.Web.UI.UserControl
{
    public string SearchPhrase { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            try
            {
                //show news from google        
                GoogleNewsDataSource.DataFile = string.Format(GoogleHelper.GoogleNewsFeedUrl, SearchPhrase);
                GoogleNewsListView.DataSourceID = GoogleNewsDataSource.UniqueID;
                GoogleNewsListView.DataBind();

            }
            catch (Exception)
            {



            }
            try
            {
                //show blogsearch
                BlogSearchDataSource.DataFile = string.Format(GoogleHelper.BlogSearchFeedUrl, SearchPhrase);
                BlogSearchListView.DataSourceID = BlogSearchDataSource.UniqueID;
                BlogSearchListView.DataBind();
            }
            catch (Exception)
            {


            }
            //show news from live com
            try
            {
                LiveNewsDataSource.DataFile = string.Format(LiveHelper.LiveNewsFeedUrl, SearchPhrase);
                LiveNewsListView.DataSourceID = LiveNewsDataSource.UniqueID;
                LiveNewsListView.DataBind();
            }
            catch (Exception)
            {

                throw;
            }


            //search results from live.com
            try
            {
                LiveSearchDataSource.DataFile = string.Format(LiveHelper.LiveSearchFeedUrl, SearchPhrase);
                LiveSearchListView.DataSourceID = LiveSearchDataSource.UniqueID;
                LiveSearchListView.DataBind();
            }
            catch (Exception)
            {

                //todo
            }


            
            try
            {
                LoadVideos(this.SearchPhrase);
                LoadPicasaImages();
                LoadFlickerImages();

            }
            catch (Exception)
            {


            }
            SetTheCorrectTabIndex();

        }
        SetPlayerPosition();
        SetPlayerPositionHorizontal();


    }

    private void SetTheCorrectTabIndex()
    {
        if (GoogleNewsListView.Items.Count == 0)
        {
            MainTabContainer.ActiveTabIndex = 5;
            if (LiveSearchListView.Items.Count == 0)
            {
                MainTabContainer.ActiveTabIndex = 4;
                if (BlogSearchListView.Items.Count == 0)
                {
                    MainTabContainer.ActiveTabIndex = 3;
                }
            }

        }
    }

    private void LoadFlickerImages()
    {
        Photos photos = FlickrHelper.GetPhotosByKeyword(this.SearchPhrase);
        FlickerImagesListView.DataSource = photos.PhotoCollection;
        FlickerImagesListView.DataBind();
    }

    private void LoadPicasaImages()
    {
        AtomEntryCollection images = PicasaHelper.GetImagesByPhrase(this.SearchPhrase);
        PicasaImagesListView.DataSource = images;
        PicasaImagesListView.DataBind();
    }

    private void LoadVideos(string keyword)
    {
        try
        {
            string username = "seniordotnetdeveloper@gmail.com";
            string password = "dessi77&";
            string application = "buzzhour";
            string clientID = "ytapi-NikolayYordanov-buzzhour-673umn97-1";
            string developerKey = "AI39si65gkXT57qFsN8zVZiAMVRpySX9h_TVRKBS5F3bGrO0c06YuF6ISMhX2BQZyZp2MxMa_JOtcftvSeiA1kqIpzN2CulfYA";

            YouTubeService service = new YouTubeService(application, clientID, developerKey);
            service.setUserCredentials(username, password);

            YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);
            query.Query = keyword;
            query.NumberToRetrieve = 18;
            YouTubeFeed videoFeed = service.Query(query);
            VideosListView.DataSource = videoFeed.Entries;
            VideosListView.DataBind();
            if (videoFeed.Entries.Count > 0)
            {
                VideosListView.SelectedIndex = 0;
                VideosListView_SelectedIndexChanged(VideosListView, new EventArgs());
            }
        }
        catch (Exception)
        {

        }

    }


    protected void VideosListView_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (VideosListView.Items[VideosListView.SelectedIndex].ItemType == ListViewItemType.DataItem)
        {

            ListViewDataItem data = VideosListView.Items[VideosListView.SelectedIndex] as ListViewDataItem;
            ImageButton VideoThumbNail = VideosListView.Items[VideosListView.SelectedIndex].FindControl("VideoThumbNail") as ImageButton;
            SetPlayerSize();
            VideoPlayerSource.Text = VideoPlayerSource.Text.Replace("MovieId", VideoThumbNail.DescriptionUrl);

            Label videoTitle = VideosListView.Items[VideosListView.SelectedIndex].FindControl("VideoTitle") as Label;
            VideoTitleLabel.Text = videoTitle.Text;


        }

    }

    protected void PicasaImagesListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem dataItem = e.Item as ListViewDataItem;
            PicasaEntry picasaImage = dataItem.DataItem as PicasaEntry;
            Image ImageThumbNail = e.Item.FindControl("ImageThumbNail") as Image;
            ImageThumbNail.ImageUrl = picasaImage.Media.Thumbnails[2].Url;
            ImageThumbNail.Attributes.Add("onclick", string.Format("window.open('{0}',null,'width=650','height=350','status=yes,toolbar=no,menubar=no,location=no,resizable=yes')", ImageThumbNail.ImageUrl.Replace("s288", "s800")));
            Label ImageTitle = e.Item.FindControl("ImageTitle") as Label;
            ImageTitle.Text = picasaImage.Summary.Text;

        }
    }

    protected void VideosListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem data = e.Item as ListViewDataItem;
            YouTubeEntry entry = data.DataItem as YouTubeEntry;
            ImageButton VideoThumbNail = e.Item.FindControl("VideoThumbNail") as ImageButton;
            Label VideoTitle = e.Item.FindControl("VideoTitle") as Label;
            VideoTitle.Text = entry.Title.Text;
            VideoThumbNail.ImageUrl = entry.Media.Thumbnails[0].Url;
            try
            {
                VideoThumbNail.DescriptionUrl = entry.Media.VideoId.Value; 
            }
            catch (Exception)
            {

            }


        }
    }

    protected void VideosListView_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
    {
        if (VideosListView.Items[VideosListView.SelectedIndex].ItemType == ListViewItemType.DataItem)
        {
            VideosListView.SelectedIndex = e.NewSelectedIndex;

            e.Cancel = false;

        }
    }


    protected void FlickerImagesListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem dataItem = e.Item as ListViewDataItem;
            Photo flickrImage = dataItem.DataItem as Photo;
            Image ImageThumbNail = e.Item.FindControl("ImageThumbNail") as Image;
            ImageThumbNail.ImageUrl = flickrImage.SmallUrl;
            ImageThumbNail.Attributes.Add("onclick", string.Format("window.open('{0}',null,'width=650','height=350','status=yes,toolbar=no,menubar=no,location=no,resizable=yes')", flickrImage.MediumUrl));
            Label ImageTitle = e.Item.FindControl("ImageTitle") as Label;
            ImageTitle.Text = flickrImage.Title;

        }
    }

    protected void PlayerLocationDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        SessionManager.PlayerPosition = PlayerLocationDropDown.SelectedValue;
        SetPlayerPosition();
    }

    private void SetPlayerPosition()
    {
        switch (SessionManager.PlayerPosition)
        {
            case "Hide":
                VideoPlayerSource.Visible = false;
                VideoTitleLabel.Visible = false;
                break;
            case "Bottom":
                VideoPlayerSource.Visible = true;
                VideoTitleLabel.Visible = true;
                VideoPlayerPanel_AlwaysVisibleControlExtender.VerticalSide = AjaxControlToolkit.VerticalSide.Bottom;
                break;
            case "Middle":
                VideoPlayerSource.Visible = true;
                VideoTitleLabel.Visible = true;
                VideoPlayerPanel_AlwaysVisibleControlExtender.VerticalSide = AjaxControlToolkit.VerticalSide.Middle;
                break;
            case "Top":
                VideoPlayerSource.Visible = true;
                VideoTitleLabel.Visible = true;
                VideoPlayerPanel_AlwaysVisibleControlExtender.VerticalSide = AjaxControlToolkit.VerticalSide.Top;
                break;
            default:
                break;
        }
    }

    private void SetPlayerPositionHorizontal()
    {
        switch (SessionManager.PlayerPositionHorizontal)
        {
            case "Left":
                VideoPlayerPanel_AlwaysVisibleControlExtender.HorizontalSide = AjaxControlToolkit.HorizontalSide.Left;
                break;
            case "Center":
                VideoPlayerPanel_AlwaysVisibleControlExtender.HorizontalSide = AjaxControlToolkit.HorizontalSide.Center;
                break;
            case "Right":
                VideoPlayerPanel_AlwaysVisibleControlExtender.HorizontalSide = AjaxControlToolkit.HorizontalSide.Right;
                break;
            default:
                break;
        }
    }

    private void SetPlayerSize()
    {
        switch (SessionManager.PlayerSize)
        {
            case "Large":
                VideoPlayerSource.Text = ComplexStrings.YouTubePlayerNormalSize;
                break;
            case "Medium":
                VideoPlayerSource.Text = ComplexStrings.YouTubePlayerSmallResolution;
                break;
            default:
                break;
        }
    }

    protected void PlayerSizeDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        SessionManager.PlayerSize = PlayerSizeDropDown.SelectedValue;
        SetPlayerSize();
        VideosListView_SelectedIndexChanged(VideosListView, new EventArgs());
    }

    protected void PlayerHorizontalLocationDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        SessionManager.PlayerPositionHorizontal = PlayerHorizontalLocationDropDown.SelectedValue;
        SetPlayerPositionHorizontal();
    }

    protected void GoToVideosTabImageButton_Click(object sender, ImageClickEventArgs e)
    {
        this.MainTabContainer.ActiveTabIndex = 3;
    }
}
